package ru.sazonov.ide.model.java

import ru.sazonov.ide.model.Artefact

class Klass extends Artefact {
    def String fullName;    // supported
    def String name;    // supported
    
    def List<Method> methods = [];  // supported
    def List<Property> javaProperties = [];

    def List<Type> doesImplements = [];   // supported
    def List<Type> doesExtends = [];  // supported

    def List<Ctor> ctors = []

    def boolean overridesToString;
    def boolean overridesEquals;
    def boolean overridesHash;
    def boolean hasEmpyPublicCtor;
    def boolean hasParametrizedCtor;

    @Override
    String toString() {
        fullName;
    }

    def fix() {
        javaProperties = initJavaProperties()
        overridesToString = methods.find {it.name == 'toString'} != null;
        overridesEquals = methods.find {it.name == 'equals'} != null;
        overridesHash = methods.find {it.name == 'hash'} != null;
        this;
    }

    private List<Property> initJavaProperties() {
        List<Property> result = new ArrayList<Property>();
        Set<String> names = new HashSet<String>();
        for (method in methods) {
            if (method) {
                if (method.name.length() > 3 &&
                        ((method.args.size() == 0 && method.name.startsWith("get")) ||
                                (method.name.startsWith("set") && method.args.size() == 1))) {
                    String name = method.name.substring(3);
                    char c = name[0].toLowerCase();
                    name = String.valueOf(c) + name.substring(1);
                    names.add(name);
                }
            }
        }

        for (name in names) {
            result.add(new Property(
                    name: name,
                    read: methods.find {it.name == "get${name[0].toUpperCase()}${name.substring(1)}"} != null,
                    write: methods.find {it.name == "set${name[0].toUpperCase()}${name.substring(1)}"} != null
            ));
        }

        result;
    }
}
